const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtracTextPlugin = require('extract-text-webpack-plugin');

let WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev';
console.log(WEBPACK_ENV);

module.exports = {
  entry: './src/app.jsx',
  devtool: 'source-map',
  output: {
    path: path.resolve(__dirname, 'dist'),
    publicPath: WEBPACK_ENV === 'dev' ? '/dist/' : '/',
    filename: 'js/app.js'
  },
  resolve:{
   alias :{
     page:path.resolve(__dirname, 'src/page'),
     component:path.resolve(__dirname,'src/component'),
     tool:path.resolve(__dirname,'src/tool'),
     service:path.resolve(__dirname,'src/service'),
   }
  },
  module: {
    rules: [
      //react(jsx)语法处理
      {
        test: /\.jsx$/,
        exclude: /(node_modules)/, 
        use: {
           loader: 'babel-loader',
           options: {
                  presets: ['env','react']
               }
            }
        },
        //css文件处理
        {
          test: /\.css$/,
          use: ExtracTextPlugin.extract({
               fallback:"style-loader",
               use:"css-loader"
          })
        },
        //sass文件处理
        {
          test: /\.scss$/,
          use: ExtracTextPlugin.extract({
               fallback:"style-loader",
               use:['css-loader','sass-loader']
          })
        },
        //图片处理
        {
          test: /\.(png|jpg|gif)$/i,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 8192,
                name:'resource/[name].[ext]'
              }
            }
          ]
        },
           //字体图标配置
         {
          test: /\.(eot|svg|woff|woff2|otf|ttf)$/,
          use: [
            {
              loader: 'url-loader',
              options: {
                limit: 8192,
                name:'resource/[name].[ext]'
              }
            }
          ]
        },
      ]
  },
  plugins: [
    //处理html文件
    new HtmlWebpackPlugin({
      template:'./src/index.html',
      favicon:'./favicon.ico'
    }),
    //独立css文件
    new ExtracTextPlugin("css/[name].css"),
    //提取公共模块
    new webpack.optimize.CommonsChunkPlugin({
      name : 'common',
      filename :'js/base.js'
    })
  ],
  devServer: {
    port:9090,
    historyApiFallback:{
      index:'/dist/index.html'
    },
    proxy :{
      '/manage' :{
          target: 'http://admintest.happymmall.com',
          changeOrigin:true
      },
      '/user/logout.do' :{
          target: 'http://admintest.happymmall.com',
          changeOrigin:true
      }
    }
   },
};